package com.example.psychologicalappointmentsystem.service.impl.questionnaire;

import com.example.psychologicalappointmentsystem.Controller.QuestionnaireController;
import com.example.psychologicalappointmentsystem.service.QuestionnaireService;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
/**
 * ClassName QuestionnaireImpl
 * Description TODO
 * Author feller
 * Date 2025/2/17 16:37
 * Version 1.0
 */
@Service
public class QuestionnaireServiceImpl implements QuestionnaireService {
    @Override
    // 调用情感分析模型接口
    public String analyzeSentiment(String text) {
        try {
            String jsonPayload = "{\"text\": \"" + text + "\"}";

            // 发送请求到情感分析模型接口
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> entity = new HttpEntity<>(jsonPayload, headers);
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<Map> response = restTemplate.exchange(QuestionnaireController.SENTIMENT_ANALYSIS_URL, HttpMethod.POST, entity, Map.class);

            // 返回情感分析结果
            Map result = response.getBody();
            return result != null ? (String) result.get("sentiment") : "unknown";
        } catch (Exception e) {
            return "分析失败";
        }
    }
}
